<?php


namespace app\admin\controller;
use app\common\lib;
use think\Cache;
use think\Db;

class Index extends Base
{
    public function index()
    {
        return $this->fetch();
    }

    public function index_v1()
    {
        //获取最新年份
        $arr_year = $this->get_new_year();
        //基本报道信息
        if(!$arr_year) {
            $new_count = [  //报道数据汇总
                'new_year'          => '0', //报道年份
                'new_count'         => '0', //总报道人数
                'new_report_count'  => '0', //待报道人数
                'new_status_count'  => '0', //已报道人数
                'new_error_count'   => '0', //异常学生
                'percent'           => '0'
            ];
        }else {
            $new_count['new_year'] = $arr_year['0']['name'];
            $new_count['new_count'] = Model('Student')->where(['stu_year' => $arr_year['0']['name']])->cache(true, '3600')->count(); //总报道人数
            $new_count['new_report_count'] = Model('Student')->where(['stu_year' => $arr_year['0']['name'], 'status' => '1'])->cache(true, '3600')->count(); //待报道人数
            $new_count['new_status_count'] = Model('Student')->where(['stu_year' => $arr_year['0']['name'], 'status' => '2'])->cache(true, '3600')->count(); //已报道人数
            $new_count['new_error_count'] = Model('Student')->where(['stu_year' => $arr_year['0']['name'], 'status' => '3'])->cache(true, '3600')->count(); //异常学生
            $new_count['percent'] =  ($new_count['new_status_count']/$new_count['new_count'])*100 ;//百分比
        }
        $comm_sql =  new lib\Commonsql();
        #halt($comm_sql->group_by_level('2',$arr_year['0']['name']));
        $college_data = $comm_sql->group_by_level('2',$arr_year['0']['name']);
        //登录信息
        $user_msg = $this->isLogin();
        if(!$user_msg) {
            $this->error('信息过期', 'admin/login/index');
        }
        $map['uid'] = $user_msg['id'];
        #$login_msg = Db::name('user_login_msg')->page('1','5')->where($map)->select();
        $login_msg = Model('UserLoginMsg')->where($map)->cache(true,'7200')->order(['id'=>'desc'])->paginate(12);
        $this->assign([
            'new_count'    => $new_count,
            'college_data' => $college_data,
            'login_msg'    => $login_msg
        ]);
        return $this->fetch();
    }

    public function logout()
    {
        $isLogin = lib\Tools::isLogin();
        if($isLogin) {
            $_login_msg = lib\Tools::get_login_msg($isLogin['id'],'1','退出登录');
            Model('UserLoginMsg')->save($_login_msg);
            session(null,config('tools.session')['session']);
            Cache::clear();
            del_dir_files('./qrcode/');
            return $this->success('退出成功','admin/login/index');
        }
        return $this->error('非法操作','admin/login/index');
    }
}